package edu.chinasoft.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.annotation.WebServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import edu.chinasoft.domain.Role;
import edu.chinasoft.domain.User;
import edu.chinasoft.service.ApplicationService;
import edu.chinasoft.util.Dom4jUtil;

/**
 *
 * @author mhao
 * @date 2020年2月12日:下午3:56:48
 * @version V1.0
 * @Description:
 */
@WebServlet(name = "myGetUserForAddApplicationServlet", value = "/servlet/getuserforaddapplicationservlet")
public class GetUserForAddApplicationServlet extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(GetUserForAddApplicationServlet.class);
	private ApplicationService applicationService;

	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" GetUserForAddApplicationServlet >doGet start.......");
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		ApplicationService applicationServiceImp = wac.getBean("applicationService", ApplicationService.class);
		this.applicationService = applicationServiceImp;
		// 获取当前登录用户
		HttpSession session = request.getSession();
		User userLogin = (User) session.getAttribute("userLogin");
		// 获取当前登录用户的角色
		Role loginRole = applicationService.selectRoleByUser(userLogin);
		logger.info("当前登录用户的角色为：" + loginRole);
		// 获取xml文件的绝对路径
		String path = "/config/flow.xml";
		String realPath = request.getServletContext().getRealPath(path);
		List<String> roleNameList = Dom4jUtil.getContent(realPath);
		int index = roleNameList.indexOf(loginRole.getRoleName());
		String roleName = roleNameList.get(index + 1);
		Role role = new Role();
		role.setRoleName(roleName);
		logger.info("下一级审批者的角色为：" + role);
		// 获取角色对应的用户
		List<User> userList = applicationService.selectUserByRole(role);
		session.setAttribute("applicationService", userList);
		response.sendRedirect(request.getContextPath() + "/addApplication.jsp");
		this.doGet(request, response);
		logger.info(" GetUserForAddApplicationServlet >doGet end.......");
	}

	/**
	 *
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" GetUserForAddApplicationServlet >doPost start.......");

		logger.info(" GetUserForAddApplicationServlet >doPost end.......");
	}

}